package com.yundingai.controller;

import com.yundingai.config.MinioConfig;
import com.yundingai.domain.entity.AllUser;
import com.yundingai.service.AllUserService;
import com.yundingai.utils.MinIOUtil;
import com.yundingai.vm.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private AllUserService allUserService;
    @Autowired
    private MinioConfig minioConfig;
    @Autowired
    private MinIOUtil minIOUtil;

    /**
     * 文件上传保存到指定位置同时返回可以访问当前文件网络地址
     */
//    @PostMapping("/upload")
//    public ResponseResult upload(@RequestParam("file") MultipartFile file) {
//        String fileName = file.getOriginalFilename();
//        String filePath = "F:/360Downloads/pro3stage/yunding-ai/src/main/resources/static/images/" + fileName;
//        try {
//            file.transferTo(new File(filePath));
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return ResultObj.ok().data("filePath", "/images/" + fileName);
//    }

    @PostMapping("/upload")
    public ResponseResult upload(@RequestParam("file") MultipartFile file,@RequestParam("userId") Integer userId) {
        log.info("上传文件：{}", file.getOriginalFilename());
        // 只调用一次上传方法
        String fileName = minIOUtil.upload(file);

        // 构建文件访问URL
        String fileUrl = minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName;

        // 更新用户头像URL
        allUserService.updateAvatarUrl(userId, fileUrl);
        Map<String, String> map = new HashMap<>();
        map.put("url", fileUrl);
        map.put("fileName", fileName);
        return ResponseResult.okResult(map);
    }
    //修改头像的方法
    @PostMapping("/updateAvatar")
    public ResponseResult updateAvatar(@RequestParam("userId") Integer userId, @RequestParam("avatarFile") MultipartFile avatarFile) throws IOException {
        //1.指定上传文件保存位置
        String basePath = "E://img/";
        //2.定义文件名称前缀
        String file_pregix = "WNXY_";
        //指定基础网络地址
        String baseUrl = "http://localhost:80/";

        //3.获取上传文件原名称
        String originalFilename = avatarFile.getOriginalFilename();
        //截取字符串  //ss.ss.aa.jpg
        int index = originalFilename.lastIndexOf(".");
        String suffix = originalFilename.substring(index);

        //4.随机生成文件名称
        long prefix = new Date().getTime();
        //5.拼接文件名称
        String fileName = file_pregix + prefix + suffix;
        System.out.println(fileName);

        //拼接网络路径
        String avatarUrl = baseUrl + fileName;

        //6.把文件保存到指定路径下
        avatarFile.transferTo(new File(basePath, fileName));
        //7.调用service层方法把文件保存到数据库中
        allUserService.updateAvatarUrl(userId, avatarUrl);
        //8.把访问图片网络路径响应给前端

        return ResponseResult.okResult(avatarUrl);
    }
//    获取用户总数 (已移动至StatisticController)
//    public ResponseResult<AllUser> getTotalUsers() {
    /**
     * @Author zhaoj
     * @Date 2025/7/22 16:11
     * @Description 根据id查询用户的基本信息
     * @Params [user] (userId)
     * @Return AllUser对象
     **/
    @PostMapping("getUserInfo")
    public ResponseResult<AllUser> getUserInfo(@RequestBody AllUser user) {
        return ResponseResult.okResult(allUserService.getById(user.getUserId()));

    }
    @PutMapping("updateUserInfo")
    public ResponseResult updateUserInfo(@RequestBody AllUser allUser) {
        allUserService.updateUserInfo(allUser);
        return ResponseResult.okResult();
    }
    @PutMapping("updatePassword")
    public ResponseResult updatePassword(@RequestBody AllUser allUser) {
        allUserService.updatePassword(allUser.getUserId(), DigestUtils.md5DigestAsHex(allUser.getPassword().getBytes()));
        return ResponseResult.okResult();
    }

}
